package org.jeecg.modules.minqing.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.util.UUIDGenerator;
import org.jeecg.modules.minqing.entity.Plot;
import org.jeecg.modules.minqing.entity.Project;
import org.jeecg.modules.minqing.mapper.PlotMapper;
import org.jeecg.modules.minqing.service.IPlotService;
import org.jeecg.modules.minqing.service.IProjectService;
import org.jeecg.modules.minqing.vo.PlotMoudule;
import org.jeecg.modules.minqing.vo.ProjectMouduleVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 地块管理
 * @Author: jeecg-boot
 * @Date:   2020-03-20
 * @Version: V1.0
 */
@Service
public class PlotServiceImpl extends ServiceImpl<PlotMapper, Plot> implements IPlotService {
    @Autowired
    private IProjectService projectService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean delProjectAndPlot(Project project) {
        Plot parent =  this.getOne(new LambdaQueryWrapper<Plot>().eq(Plot::getIsParent,"1").eq(Plot::getBelong,project.getId()));
        if (ObjectUtil.isEmpty(parent)){
            throw new JeecgBootException("主地块不存在");
        }
        List<Plot> sonPlots = this.list(new LambdaQueryWrapper<Plot>().eq(Plot::getIsParent,"0").eq(Plot::getBelong,parent.getId()));
        this.removeById(parent.getId());
        for (Plot son:sonPlots){
            this.removeById(son.getId());
        }
        project.setDelFlag("1");
        projectService.updateById(project);
        return true;
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean editProjectAndPlot(Project project) {
        Plot parent =  this.getOne(new LambdaQueryWrapper<Plot>().eq(Plot::getIsParent,"1").eq(Plot::getBelong,project.getId()));
        if (ObjectUtil.isNotEmpty(parent)) {
            //删除旧地块
            this.removeById(parent.getId());
            List<Plot> sonPlots = this.list(new LambdaQueryWrapper<Plot>().eq(Plot::getIsParent,"0").eq(Plot::getBelong,parent.getId()));
            for (Plot son:sonPlots){
                this.removeById(son.getId());
            }
        } else {
            parent = new Plot();
            parent.setId(UUIDGenerator.generate());
            parent.setIsParent("1");
            parent.setBelong(project.getId());
            parent.setPlotName(project.getProjectName());
            parent.setPlotImg(project.getImg());
        }
        List<ProjectMouduleVO>  projectMouduleVOList = JSONUtil.toList(JSONUtil.parseArray(project.getProjectMoudle()),ProjectMouduleVO.class);
        this.save(parent);
        for (ProjectMouduleVO mouduleVO:projectMouduleVOList){
            List<PlotMoudule> list = mouduleVO.getList();
            if (list==null|| list.size()==0){
                continue;
            }
            List<Plot> plotList = new ArrayList<>();
            for (PlotMoudule moudule:list){
                Plot plot = new Plot();
                plot.setId(UUIDGenerator.generate());
                plot.setBelong(parent.getId());
                plot.setIsParent("0");
                plot.setPlotImg(mouduleVO.getAreaImg());
                plot.setBelongRegion(mouduleVO.getName());
                plot.setPlotName(moudule.getName());
                plot.setPlotType(moudule.getType());
                plot.setAreaSize(moudule.getArea());
                if ("1".equals(moudule.getStatus())){
                    plot.setStatus("已报批土地中");
                } else {
                    plot.setStatus("已规划未报批");
                }
                plot.setPlotContent(moudule.getSynopsis());
                plotList.add(plot);
            }
            this.saveBatch(plotList);
        }
        projectService.updateById(project);
        return true;
    }



    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addProjectAndPlot(Project project) {
        project.setId(UUIDGenerator.generate());
//        Plot parent =  this.getOne(new LambdaQueryWrapper<Plot>().eq(Plot::getIsParent,"1").eq(Plot::getBelong,project.getId()));
//        if (ObjectUtil.isNotEmpty(parent)){
//            throw new JeecgBootException("数据异常");
//        }
        List<ProjectMouduleVO>  projectMouduleVOList = JSONUtil.toList(JSONUtil.parseArray(project.getProjectMoudle()),ProjectMouduleVO.class);
        Plot parent = new Plot();
        parent.setId(UUIDGenerator.generate());
        parent.setIsParent("1");
        parent.setBelong(project.getId());
        parent.setPlotName(project.getProjectName());
        parent.setPlotImg(project.getImg());
        this.save(parent);
        for (ProjectMouduleVO mouduleVO:projectMouduleVOList){
            List<PlotMoudule> list = mouduleVO.getList();
            if (list==null|| list.size()==0){
                continue;
            }
            List<Plot> plotList = new ArrayList<>();
            for (PlotMoudule moudule:list){
                Plot plot = new Plot();
                plot.setId(UUIDGenerator.generate());
                plot.setBelong(parent.getId());
                plot.setIsParent("0");
                plot.setPlotImg(mouduleVO.getAreaImg());
                plot.setBelongRegion(mouduleVO.getName());
                plot.setPlotName(moudule.getName());
                plot.setPlotType(moudule.getType());
                plot.setAreaSize(moudule.getArea());
                if ("1".equals(moudule.getStatus())){
                    plot.setStatus("已报批土地中");
                } else {
                    plot.setStatus("已规划未报批");
                }
                plot.setPlotContent(moudule.getSynopsis());
                plotList.add(plot);
            }
            this.saveBatch(plotList);
        }
        projectService.save(project);
        return true;
    }





}
